package com.liufei1988.design.singleton;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;

public class FileLogger {
    private String path="d:/jbd/log.txt";
    private FileOutputStream out;
    private static FileLogger log;

    private FileLogger(){
        try {
            out=new FileOutputStream(path,true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("This is new instance");
    }
//    private static FileLogger
//    private static class My{
//        static FileLogger log;
//        static {
//            try {
//                log=new FileLogger();
//            }catch (Exception e){
//                e.printStackTrace();
//            }
//        }
//    }
    public static FileLogger getInstance(){
        log=new FileLogger();
        return log;
    }
    public  void write(String msg){
        try {
        Calendar c=Calendar.getInstance();
        int y=c.get(Calendar.YEAR);
        int n= c.get(Calendar.MARCH);
        int d=c.get(Calendar.DAY_OF_MONTH);
        int hh=c.get(Calendar.HOUR);
        int mm=c.get(Calendar.MINUTE);
        String strTime="";
        strTime=strTime.format("time:%d-%d-%d %d:%d",y,n,d,hh,mm);
        String strContent="content:\r\n"+msg+"\r\n";
            byte[] buf=strTime.getBytes("gbk");
            out.write(buf);
            buf=strContent.getBytes("gbk");
            out.write(buf);
            out.flush();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void close(){
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
